package cn.zzf.leetcode;

public class ListNode {
    int val;
    ListNode next;

    ListNode() {}

    ListNode(int val) {this.val = val;}

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        return "[" + toString_() + "]";
    }

    public String toString_() {
        return next == null ? String.valueOf(val) : val + ", " + next.toString_();
    }

    public static ListNode of(int... ints) {
        if(ints == null || ints.length == 0) {
            return null;
        }

        ListNode param = new ListNode();
        ListNode current = param;
        ListNode tmp = new ListNode();
        param.val = ints[0];
        for (int i = 1; i < ints.length; i++) {
            tmp.val = ints[i];
            current.next = tmp;
            current = tmp;
            tmp = new ListNode();
        }
        return param;
    }
}
